const app = getApp();
const Router = require('../../common/core/router.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    len: 0
  },
  ComponentListener(e) {
    let authurl = app.Config.AuthDomain;
    let func = e.detail.func;
    //console.log(func)
    if (func == 'fpub') {
      wx.navigateTo({
        url: '../mpub/mpub?version=2',
      })
    } else if (func == 'reqdata') {
      this.ReqData();
    } else if (func == 'auth') {
      let token = wx.getStorageSync('pubtoken') || 0;
      //console.log(token)
      let pwd = e.detail.pwd;
      //  console.log(pwd)
      if (pwd == undefined) {
        if (token == 0) {
          this.setData({
            len: 6
          })
        } else {
          wx.request({
            url: authurl,
            success: res => {
              console.log('返回了结果')
              let list = res.data.token_list;
              var flag = false;
              for (var i = 0; i < list.length; i++) {
                console.log(list[i])
                if (list[i] == pwd || list[i] == token) {

                  flag = true;
                  //wx.setStorageSync('pubtoken', pwd);

                }
              }
              if (flag) {
                wx.navigateTo({
                  url: '../mpub/mpub?version=2',
                })
              } else {
                this.setData({
                  len: 6
                })
              }

            }
          })
        }
      } else {
        wx.request({
          url: authurl,
          success: res => {
            let list = res.data.token_list;
            for (var i = 0; i < list.length; i++) {
              if (list[i] == pwd) {
                wx.setStorageSync('pubtoken', pwd);
                wx.navigateTo({
                  url: '../mpub/mpub?version=2',
                })
              }
            }
          }
        })
      }

    } else if (func == 'clean') {
      this.setData({
        len: 0
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let SevMode = app.Config.SevMode;
    this.setData({
      version: app.Config.Version
    })
    //console.log(SevMode)
  },
  ReqData() {
    //数据请求
    wx.showLoading({
      title: '请求数据中~'
    })
    let step = this.data.list.length;
    Router.QueryCircleInfo(step).then(res => {
      console.log(res.data)
      this.setData({
        list: this.data.list.concat(res.data)
      }, () => {
        wx.stopPullDownRefresh();
        wx.hideLoading();
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list: []
    }, () => {
      this.ReqData();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.ReqData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})